var dbConfig = require('../util/dbconfig');
const dao = require('../util/dao');


// 查询所有商品 分页显示
var getAllGoods = (req, res) => {

    var obj = req.query

    var f = dao.fen(obj.pages, obj.size)
    var sql = 'select * from goods limit ?,?';
    var sqlArr = [f.p, f.s];
    var callBack = (err, data) => {
        if (err) {
            console.log(err);
            res.send(dao.fail(500, '失败'))
        }
        console.log(data)
        // 总数据量
        var sqli = 'select count(*) as sum from goods'
        var callBackii = (err, r) => {
            if (err) {
                res.send(dao.fail(500, '失败'))
            }
            var t = r[0].sum
            console.log(t);
            // 计算总页数=总数据量/每页显示的数据量
            var z = Math.ceil(t / obj.size)
            console.log(z);
            res.send(dao.pageok(data, t, z, obj.pages))
        }
        dbConfig.sqlConnect(sqli, null, callBackii)

    }
    dbConfig.sqlConnect(sql, sqlArr, callBack)
}

//根据品牌查询商品
var getSortGoods = (req, res) => {
    var obj = req.params
    var f = dao.fen(obj.pages, obj.size)
    var sql = 'SELECT goods.* from emps_goods' + ' LEFT JOIN goods on emps_goods.goods_id=goods.id' + ' WHERE emps_goods.emps_id =? limit ?,?'
    var sqlArr = [obj.uid, f.p, f.s]
    var callBack = (err, data) => {
        if (err) {
            res.send(dao.fail(500, '失败'))
            return
        }
        var sqls = 'select count(*) as sum from emps_goods' + ' LEFT JOIN goods on emps_goods.goods_id=goods.id' + ' WHERE emps_goods.emps_id =?'
        var arr = [obj.uid]
        var back = (err, d) => {
            if (err) {
                res.send(dao.fail(500, '失败'))
                return
            }
            var t = d[0].sum
            var z = Math.ceil(t / obj.size)
            res.send(dao.pageok(data, t, z, obj.pages))
        }
        dbConfig.sqlConnect(sqls, arr, back)
    }
    dbConfig.sqlConnect(sql, sqlArr, callBack)

}

// 根据分类查询商品
var getClassGoods = (req, res) => {
    var obj = req.params
    console.log('啊啊啊',obj.size);
    var f = dao.fen(obj.page,obj.size)
    var sql = 'SELECT gs.* FROM emps_goods AS eg' + ' LEFT JOIN goods AS gs on gs.id = eg.goods_id' + ' WHERE eg.emps_id IN (SELECT id FROM emps WHERE parent_id = ?) limit ?,?'
    var sqlArr = [obj.id, f.p, f.s]
    console.log('sqlarr',sqlArr);
    var callBack = (err, data) => {
        if (err) {
            res.send(dao.fail(500, '失败'))
            return
        }
        var sqlsss = 'select count(*) as sum FROM emps_goods AS eg' + ' LEFT JOIN goods AS gs on gs.id = eg.goods_id' + ' WHERE eg.emps_id IN (SELECT id FROM emps WHERE parent_id = ?)'
        var arrrr = [obj.id]
        var backkk = (err, d) => {
            if (err) {
                res.send(dao.fail(500, '失败'))
                return
            }
            var t = d[0].sum
            var z = Math.ceil(t / obj.size)
            res.send(dao.pageok(data, t, z, obj.page))
        }
        dbConfig.sqlConnect(sqlsss, arrrr, backkk)
    }
    dbConfig.sqlConnect(sql, sqlArr, callBack)

}

// 分类页面 模糊搜索
var getClassGoodsLike=(req,res)=>{
    var obj = req.body
    console.log(obj);
    var f = dao.fen(obj.page,obj.size,)
    console.log('f',f);
    var sqlArr = [obj.id,"%" + obj.goods_name + "%",f.p,f.s]
    console.log('sqlarr',sqlArr);
    var sql = 'SELECT gs.* FROM emps_goods AS eg' + ' LEFT JOIN goods AS gs on gs.id = eg.goods_id' + ' WHERE eg.emps_id IN (SELECT id FROM emps WHERE parent_id = ?) and gs.goods_name like ? limit ?,?'
    

    var callBack = (err, data) => {
        if (err) {
            console.log(err);
            res.send(dao.fail(500, '失败'))
            return
        }
        
       var  ssqqllaarrr=[obj.id,"%" + obj.goods_name + "%"]
       console.log('ssqqllaarrr',ssqqllaarrr);
       var ssqll='select count(*) as sum FROM emps_goods AS eg' + ' LEFT JOIN goods AS gs on gs.id = eg.goods_id' + ' WHERE eg.emps_id IN (SELECT id FROM emps WHERE parent_id = ?) and gs.goods_name like ? '
       var callllllbacck=(err,d)=>{
        console.log('d',d);
        
        if (err) {
            console.log(err);
            res.send(dao.fail(500, '失败1'))
            return
        }
        var t = d[0].sum
        console.log('t',t);
        var z = Math.ceil(t / obj.size)
        console.log('z',z);
        res.send(dao.pageok(data, t, z, obj.page))
       }
    dbConfig.sqlConnect(ssqll, ssqqllaarrr, callllllbacck)
        
        
    }
    dbConfig.sqlConnect(sql, sqlArr, callBack)
}


// 删除商品接口
var delGoods = (req, res) => {
    var obj = req.params
    var sql = 'delete from goods where id=?'
    var sqlArr = [obj.id]
    console.log(sqlArr);
    var callBack = (err, data) => {

        if (data.affectedRows === 0) {

            res.send(dao.fail(500, '失败'))

        } else {
            var sqlq = 'delete from emps_goods where goods_id=?'
            var callBackTwo = (err, data) => {
                console.log(err);
                if (data.affectedRows === 0) {
                    res.send(dao.fail(500, '失败1'))
                } else {
                    res.send(dao.ok(data))
                }
            }
            dbConfig.sqlConnect(sqlq, sqlArr, callBackTwo)
        }

    }
    dbConfig.sqlConnect(sql, sqlArr, callBack)
}
// 添加商品
var addGoods = (req, res, next) => {
    var obj = req.body
    var sqlArr = [obj.goods_name, obj.goods_price]
    var sql = 'insert into goods values (null,?,null,?,null)'
    var callBack = (err, data) => {
        if (err) {
            return next(dao.fail(500, '失败'))
        } else {
            var sqlAlls = [obj.emps_id, data.insertId]
            var sqli = 'insert into emps_goods values (?,?)'
            var callBackt = (err, data) => {
                if (err) {
                    return next(err)
                }

                res.send(dao.ok())
            }
            dbConfig.sqlConnect(sqli, sqlAlls, callBackt)

        }

    }
    dbConfig.sqlConnect(sql, sqlArr, callBack)
}

// 修改商品接口
var revampGoods = (req, res) => {
    var obj = req.body
    var sql = 'UPDATE goods SET goods_name=?,goods_price=?,goods_ico=? where id=?'
    var sqlArr = [obj.goods_name, obj.goods_price, obj.goods_ico, obj.id]
    var callBack = (err, data) => {
        console.log('0000000000000', data);
        
            res.send(dao.ok(data))
        
    }
    dbConfig.sqlConnect(sql, sqlArr, callBack)
}

// 模糊查询接口
var likeGoods = (req, res) => {
    // console.log(req);
    var obj = req.body
    console.log(obj);
    var sqlArr = ["%" + obj.goods_name + "%"]
    console.log(sqlArr);
    var sql = 'select * from goods where goods_name like ?'
    var callBack = (err, data) => {

        res.send(dao.ok(data))

    }
    dbConfig.sqlConnect(sql, sqlArr, callBack)

}

// 根据商品ID查询商品
var getIdGoods=(req,res)=>{
    var obj =req.params
    var sqlArr=[obj.id]
    console.log('ID',sqlArr);
    var sql='select * from goods where id=?'
    var callBack = (err, data) => {

        if(err){
            res.send(dao.fail(500, '失败'))
            
        }else {
            res.send(dao.ok(data[0]))
        }

    }
    dbConfig.sqlConnect(sql, sqlArr, callBack)

}



module.exports = {getAllGoods, getSortGoods, delGoods, addGoods, revampGoods, likeGoods, getClassGoods,getClassGoodsLike,getIdGoods}